<?php
class Application_Model_Mappers_PageMapper extends HelloCMS_Model_Mapper {
	protected $_dbTableName = 'Application_Model_DbTables_Page';
	
	public function fetchAll(){
		$result = $this->getDbTable()->fetchAll();
		$entries = array();
		foreach($result as $row){
			$entry = new Application_Model_Models_Page();
			echo $row->title;
			$entry->setId($row->id)
			->setTitle($row->title)
			->setLink($row->link)
			->setStatusCode($row->status_code)
			->setCreated($row->created);
			$entries[] = $entry;
		}
		return $entries;
	}
	
	public function find($id){
		$result = $this->getDbTable()->find($id);
		if(0 == count($result)){
			return null;
		}
		$row =$result->current();
		$entry = new Application_Model_Models_Page();
		$entry->setId($row->id)
			->setTitle($row->title)
			->setLink($row->link)
			->setStatusCode($row->statusCode)
			->setCreated($row->created);
		return $entry;
	}
	
	public function save(Application_Model_Models_Page $model){
		$data = array(
				'link'=>$model->getLink(),
				'title'=>$model->getTitle(),
				'status_code'=>$model->getStatusCode(),
				'created'=>date('Y-m-d H:i:s')
				);
		if(null == ($id = $model->getId())){
			unset($data['id']);
			$this->getDbTable()->insert($data);
		}else{
			$this->getDbTable()->update($data,array('id=?',$id));
		}
	}
}